package com.jindy.module.fileUploadRecord.controller;

import com.jindy.common.annotation.Log;
import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.core.page.TableDataInfo;
import com.jindy.common.enums.BusinessType;
import com.jindy.common.utils.poi.ExcelUtil;
import com.jindy.module.fileUploadRecord.domain.JinFileUploadRecord;
import com.jindy.module.fileUploadRecord.service.IJinFileUploadRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文件上传记录Controller
 * 
 * @author jindy
 * @date 2023-04-26
 */
@RestController
@RequestMapping("/module/fileUploadRecord")
public class JinFileUploadRecordController extends BaseController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(JinFileUploadRecordController.class);
    @Autowired
    private IJinFileUploadRecordService jinFileUploadRecordService;

    /**
     * 查询文件上传记录列表
     */
    @PreAuthorize("@ss.hasPermi('module:fileUploadRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(JinFileUploadRecord jinFileUploadRecord)
    {
        startPage();
        List<JinFileUploadRecord> list = jinFileUploadRecordService.selectJinFileUploadRecordList(jinFileUploadRecord);
        return getDataTable(list);
    }

    /**
     * 导出文件上传记录列表
     */
    @PreAuthorize("@ss.hasPermi('module:fileUploadRecord:export')")
    @Log(title = "文件上传记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JinFileUploadRecord jinFileUploadRecord)
    {
        List<JinFileUploadRecord> list = jinFileUploadRecordService.selectJinFileUploadRecordList(jinFileUploadRecord);
        ExcelUtil<JinFileUploadRecord> util = new ExcelUtil<JinFileUploadRecord>(JinFileUploadRecord.class);
        util.exportExcel(response, list, "文件上传记录数据");
    }

    /**
     * 获取文件上传记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:fileUploadRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(jinFileUploadRecordService.selectJinFileUploadRecordById(id));
    }

    /**
     * 新增文件上传记录
     */
    @PreAuthorize("@ss.hasPermi('module:fileUploadRecord:add')")
    @Log(title = "文件上传记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JinFileUploadRecord jinFileUploadRecord)
    {
        return toAjax(jinFileUploadRecordService.insertJinFileUploadRecord(jinFileUploadRecord));
    }

    /**
     * 修改文件上传记录
     */
    @PreAuthorize("@ss.hasPermi('module:fileUploadRecord:edit')")
    @Log(title = "文件上传记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JinFileUploadRecord jinFileUploadRecord)
    {
        return toAjax(jinFileUploadRecordService.updateJinFileUploadRecord(jinFileUploadRecord));
    }

    /**
     * 删除文件上传记录
     */
    @PreAuthorize("@ss.hasPermi('module:fileUploadRecord:remove')")
    @Log(title = "文件上传记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jinFileUploadRecordService.deleteJinFileUploadRecordByIds(ids));
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file)
    {
        try {
            return jinFileUploadRecordService.uploadFile(file);
        } catch (Exception e) {
            LOGGER.error("文件上传失败",e);
            return AjaxResult.error("文件上传失败");
        }
    }
}
